#ifndef YASAI_H
#define YASAI_H

#include <QActionGroup>
#include <QDesktopWidget>
#include <QFileDialog>
#include <QFileSystemModel>
#include <QGraphicsPixmapItem>
#include <QGraphicsScene>
#include <QLabel>
#include <QList>
#include <QMainWindow>
#include <QModelIndexList>
#include <QPersistentModelIndex>
#include <QProgressDialog>
#include <QScrollBar>
#include <QSettings>
#include <QShortcut>
#include <QStandardItemModel>
#include <QTimer>

#include "aboutdialog.h"
#include "bookmarksdialog.h"
#include "debugconsole.h"
#include "utils.h"
#include "yasaiworker.h"

//#include <QtOpenGL/QGLWidget>
//#include <QOpenGLShader>
//#include "opencv2/opencv.hpp"

namespace Ui
{
class Yasai;
}

class Yasai : public QMainWindow
{
    Q_OBJECT

public:
    explicit Yasai(QWidget *parent = 0);
    ~Yasai();
    static const int MESSAGE_TIMEOUT_MS = 2000;
    static const int PROGRESS_BAR_MIN_DURATION = 250;
    enum ScrollTo
    {
        ScrollToStart,
        ScrollToEnd
    };

public slots:
    void clearAndShowPixmap(const QPixmap &pixmap);
    void clearAndShowTwoPixmaps(const QPixmap &current, const QPixmap &next);
    void addWidePages(int widePos, int nextWidePos);
    void showModalProgressDialog(const QString &message);
    void hideModalProgressDialog();
    void archiveLoaded(QStandardItemModel *model, const QString &place);
    void openFileAndShowInBrowser(const QFileInfo &fileInfo, const QString &place);
    void refreshView();

private slots:
    void on_dirView_activated(const QModelIndex &index);
    void on_arcView_activated(const QModelIndex &index);    

    void on_actionExit_triggered();
    void on_actionOpen_triggered();

    void on_actionPageDownOrNext_triggered();
    void on_actionPageUpOrPrevious_triggered();
    void on_actionPageNextImage_triggered();
    void on_actionPagePreviousImage_triggered();
    void on_actionPageNext_triggered();
    void on_actionPagePrevious_triggered();

    void on_actionShowAbout_triggered();
    void on_actionShowBookmarks_triggered();
    void on_actionShowConsole_triggered();
    void on_actionShowToolbar_triggered();
    void on_actionShowFileBrowser_triggered();
    void on_actionShowStatusbar_triggered();

    void on_actionRotateLeft_triggered();
    void on_actionRotateRight_triggered();
    void on_actionFlipVertically_triggered();
    void on_actionFlipHorizontally_triggered();
    void on_actionTransformReset_triggered();

    void on_actionFullScreen_triggered();
    void on_actionBookmarksAddPage_triggered();
    void on_actionMouseDragToScroll_triggered();
    void on_actionMouseHideCursor_triggered();
    void moveScrollbarsToStart();
    void moveScrollbarsToEnd();

    void on_scrollToTimer();

signals:
    void openFileInWorker(const QFileInfo &fileInfo, const QString &place, const QRect &viewRect, Utils::ViewOptions viewOptions, int zoomAdjustment);
    void loadArchiveImage(int pos, int nextPos, const QRect &viewRect, Utils::ViewOptions viewOptions, int zoomAdjustment);

private:
    Ui::Yasai *ui;

    QFileSystemModel             *mFileSystemModel;
    QStandardItemModel           *mArchiveModel;
    QList<QPersistentModelIndex> *mWidePageIndexes;
    QGraphicsScene               *mGraphicsScene;
    QSettings                    *mSettings;

    QActionGroup    *fitGroup;
    QActionGroup    *directionGroup;
    QActionGroup    *pageviewGroup;
    QProgressDialog *progressModal;
    BookmarksDialog *bookmarks;
    AboutDialog     *about;
    DebugConsole    *console;

    QLabel *statusFilePath;
    QLabel *statusPage;
    QLabel *statusPlace;

    QThread     *mWorkerThread;
    YasaiWorker *mWorker;

    QShortcut *keyPageDown;
    QShortcut *keyPageUp;
    QShortcut *keyHome;
    QShortcut *keyEnd;
    QShortcut *keyD;
    QShortcut *keyU;

    Utils::ViewOptions getViewOptions();
    void setArchiveModelIndexAndActivate(const QModelIndex &index);
    void setFileSystemModelIndexAndActivate(const QModelIndex &index);
    void openFile(const QFileInfo &fileInfo, const QString &place = QString());
    void nextPage(Yasai::ScrollTo scrollto=Yasai::ScrollToStart);
    void previousPage(Yasai::ScrollTo scrollto=Yasai::ScrollToEnd);
    void moveScrollbarsTo(Yasai::ScrollTo scrollto);
    void updateStatusBar();
    void addLastRead();
    void centerDirview(int, int);

    virtual void showEvent(QShowEvent *);
    virtual void closeEvent(QCloseEvent *event);

    // save state for fullscreen
    bool isPortable;
    bool toolbarVisible;
    bool browserVisible;
    bool statusbarVisible;

    // workaround for scrollTo with QTreeView and QFileSystemModel
    bool m_arcviewWasHidden;
    QTimer *m_scrolltoTimer;
};

#endif // YASAI_H
